package com.example.collectionlinkmanagementsystem.config;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.WebExceptionHandler;

import java.util.HashMap;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/14 14:58
 */
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice  {
    /**
     * 兜底异常封装，总的异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object exceptionAdvice(Exception e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("statusCode",-1);
        hashMap.put("msg","发生了总异常 : "+e.getMessage());
        hashMap.put("data",null);
        return hashMap;
    }

    @ExceptionHandler(InvalidFormatException.class)
    public Object invalidFormatException(InvalidFormatException e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("statusCode",-1);
        hashMap.put("msg","发生了类型异常 : "+e.getMessage());
        hashMap.put("data",-2);
        return hashMap;
    }
}
